﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RG.Common.Provider;
using NHibernate.Criterion;
using NHibernate.Mapping.Attributes;
using RG.Consts;
using RealtorGuide.DAO;

namespace RG.DAO
{
    public interface IApartmentDAO
    {
        ApartmentDAOObj Get(int id);
        int Put(ApartmentDAOObj obj);
        List<ApartmentDAOObj> List(ApartmentsQueryParams param);
    }

    public class ApartmentDAO : TransactionService<RGProvider>, IApartmentDAO
    {
        public ApartmentDAOObj Get(int id)
        {
            return DAOSession.Get<ApartmentDAOObj>(id);
        }

        public int Put(ApartmentDAOObj obj)
        {
            DAOSession.Put(obj);
            return obj.Id;
        }

        public List<ApartmentDAOObj> List(ApartmentsQueryParams param)
        {
            var q = DetachedCriteria.For<ApartmentDAOObj>();

            if (!string.IsNullOrEmpty(param.ManagerName))
            {
                var qM = q.CreateCriteria("Manager");
                qM.AddString("FIO", param.ManagerName, false, false);
            }

            if (!string.IsNullOrEmpty(param.MicroAreaName))
            {
                var qA = q.CreateCriteria("MicroArea");
                qA.AddString("Name", param.MicroAreaName, false, false);
            }

            q.AddInts("CountOfRooms", param.Rooms);
            q.AddEnum("Area", param.Areas);
            q.AddEnum("TypeOfMaterial", param.Materials);
            q.AddDateRange("CreateDate", param.Date);
            q.AddEnum("State", param.States);
            q.AddString("Address",param.Address,false,false);

            q.AddRange("Cost",param.Cost);
            q.AddRange("Square", param.Square);
            q.AddRange("Floor",param.Floor);
            q.AddRange("NumberOfStoreys", param.NumberOfStoreys);
            

            return DAOSession.List<ApartmentDAOObj>(q).ToList();


        }
    }

    /// <summary>
    /// Договор хранения
    /// </summary>
    [HibernateMapping]
    [Class(Table = "Apartments")]
    public class ApartmentDAOObj : IApartment<ManagerDAOObj, MicroAreaDAOObj>
    {
        public ApartmentDAOObj()
        {
            Manager = new ManagerDAOObj();
            MicroArea = new MicroAreaDAOObj();
        }

        /// <summary>
        /// Идентификатор
        /// </summary>
        [Id(-4, Name = "Id")]
        [Column(-3, Name = "C_APARTMENT")]
        [Generator(-2, Class = "sequence")]
        [Param(-1, Name = "sequence", Content = "GEN_Apartments")]
        public virtual int Id { get; set; }

        /// <summary>
        /// Менеджер
        /// </summary>
        [ManyToOne(NotNull = true, Column = "C_Manager", Lazy = Laziness.Proxy, Fetch = FetchMode.Select)]
        public virtual ManagerDAOObj Manager { get; set; }

        /// <summary>
        /// Количество комнат
        /// </summary>
        [Property(Column = "Count_of_rooms")]
        public virtual int CountOfRooms { get; set; }

        /// <summary>
        /// Район
        /// </summary>
        [Property]
        public virtual AreasOfTheCity Area { get; set; }

        /// <summary>
        /// МикроРайон
        /// </summary>
        [ManyToOne(NotNull = true, Column = "C_Micro_Area", Lazy = Laziness.Proxy, Fetch = FetchMode.Select)]
        public virtual MicroAreaDAOObj MicroArea { get; set; }

        /// <summary>
        /// Цена
        /// </summary>
        [Property]
        public virtual decimal Cost { get; set; }

        /// <summary>
        /// Площадь
        /// </summary>
        [Property]
        public virtual decimal Square { get; set; }

        /// <summary>
        /// Этажность
        /// </summary>
        [Property(Column = "Number_Of_Storeys")]
        public virtual int NumberOfStoreys { get; set; }

        /// <summary>
        /// Этаж
        /// </summary>
        [Property]
        public virtual int Floor { get; set; }

        /// <summary>
        /// Материал
        /// </summary>
        [Property(Column = "Type_Of_Material")]
        public virtual TypeOfMaterial TypeOfMaterial { get; set; }

        /// <summary>
        /// Дата создания
        /// </summary>
        [Property(Column = "CREATE_DATE")]
        public virtual DateTime CreateDate { get; set; }

        /// <summary>
        /// Время создания
        /// </summary>
        [Property(Column = "CREATE_TIME")]
        public virtual DateTime CreateTime { get; set; }

        /// <summary>
        /// Состояние
        /// </summary>
        [Property]
        public virtual ApartmentState State { get; set; }

        /// <summary>
        /// Описание
        /// </summary>
        [Property]
        public virtual string Description { get; set; }

        /// <summary>
        /// Адрес
        /// </summary>
        [Property]
        public virtual string Address { get; set; }
    }




}
